

let wxp = <wxp.wxp>{};

declare let wx;


interface PolyfillPromise<T> {
    finally(callback?: () => T): Promise<T>;
}
interface Promise<T> extends PolyfillPromise<T> {
}




 const __assign = Object['assign'] || function (t) {
            for (var s, i = 1, n = arguments.length; i < n; i++) {
                s = arguments[i];
                for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
            }
            return t;
        };
const __rest = function (s, e) {
    var t = {};
    for (let p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object['getOwnPropertySymbols'] === "function")
        for (var i = 0, p = Object['getOwnPropertySymbols'](s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
            t[p[i]] = s[p[i]];
    return t;
};


function wxpromisify(func, context, callbackIndex) {
    if (callbackIndex === void 0) { callbackIndex = 0; }
    return function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        return new Promise(function (resolve, reject) {
            var _a = (args[callbackIndex] || {}), success = _a.success, fail = _a.fail, complete = _a.complete, arg = __rest(_a, ["success", "fail", "complete"]);
            args[callbackIndex] = __assign({}, arg, { success: function (res) {
                    resolve(res);
                    if (success)
                        success(res);
                }, fail: function (err) {
                    reject(err);
                    if (fail)
                        fail(err);
                }, complete: complete });
            func.call.apply(func, [context].concat(args));
        });
    };
}


Promise.prototype['finally'] = function (callback) {
    var P = this.constructor;
    return this.then(function (value) { return P.resolve(callback()).then(function () { return value; }); }, function (reason) { return P.resolve(callback()).then(function () { throw reason; }); });
};

var PROMISABLE = {
    "FUNCS":[
        "request",
        "uploadFile",
        "downloadFile",
        "connectSocket",
        "sendSocketMessage",
        "closeSocket",
        "chooseImage",
        "previewImage",
        "getImageInfo",
        "saveImageToPhotosAlbum",
        "startRecord",
        "playVoice",
        "getBackgroundAudioPlayerState",
        "playBackgroundAudio",
        "seekBackgroundAudio",
        "getAvailableAudioSources",
        "chooseVideo",
        "saveVideoToPhotosAlbum",
        "loadFontFace",
        "saveFile",
        "getSavedFileList",
        "getSavedFileInfo",
        "removeSavedFile",
        "openDocument",
        "getFileInfo",
        "setStorage",
        "getStorage",
        "getStorageInfo",
        "removeStorage",
        "getLocation",
        "chooseLocation",
        "openLocation",
        "getSystemInfo",
        "getNetworkType",
        "setScreenBrightness",
        "getScreenBrightness",
        "vibrateLong",
        "vibrateShort",
        "startAccelerometer",
        "stopAccelerometer",
        "startCompass",
        "stopCompass",
        "makePhoneCall",
        "scanCode",
        "setClipboardData",
        "getClipboardData",
        "openBluetoothAdapter",
        "closeBluetoothAdapter",
        "getBluetoothAdapterState",
        "startBluetoothDevicesDiscovery",
        "stopBluetoothDevicesDiscovery",
        "getBluetoothDevices",
        "getConnectedBluetoothDevices",
        "createBLEConnection",
        "closeBLEConnection",
        "getBLEDeviceServices",
        "getBLEDeviceCharacteristics",
        "readBLECharacteristicValue",
        "writeBLECharacteristicValue",
        "notifyBLECharacteristicValueChange",
        "startBeaconDiscovery",
        "stopBeaconDiscovery",
        "getBeacons",
        "setKeepScreenOn",
        "addPhoneContact",
        "getHCEState",
        "startHCE",
        "stopHCE",
        "sendHCEMessage",
        "startWifi",
        "stopWifi",
        "connectWifi",
        "getWifiList",
        "setWifiList",
        "getConnectedWifi",
        "showToast",
        "showLoading",
        "showModal",
        "showActionSheet",
        "setTopBarText",
        "setNavigationBarTitle",
        "setNavigationBarColor",
        "setTabBarBadge",
        "removeTabBarBadge",
        "showTabBarRedDot",
        "hideTabBarRedDot",
        "setTabBarStyle",
        "setTabBarItem",
        "showTabBar",
        "hideTabBar",
        "navigateTo",
        "redirectTo",
        "reLaunch",
        "switchTab",
        "startPullDownRefresh",
        "getExtConfig",
        "login",
        "checkSession",
        "authorize",
        "getUserInfo",
        "requestPayment",
        "showShareMenu",
        "hideShareMenu",
        "updateShareMenu",
        "getShareInfo",
        "chooseAddress",
        "addCard",
        "openCard",
        "openSetting",
        "getSetting",
        "getWeRunData",
        "navigateToMiniProgram",
        "navigateBackMiniProgram",
        "chooseInvoiceTitle",
        "checkIsSupportSoterAuthentication",
        "startSoterAuthentication",
        "checkIsSoterEnrolledInDevice",
        "setEnableDebug"
    ],
    "KLASS":{
        "SocketTask":[
            "connectSocket",
            "send",
            "close"
        ]
    }
};


Object.getOwnPropertyNames(wx).forEach(function (key) {
    var desc = Object.getOwnPropertyDescriptor(wx, key);
    if (desc) {
        if (PROMISABLE.FUNCS.indexOf(key) >= 0) {
            Object.defineProperty(wxp, key, {
                configurable: desc.configurable,
                enumerable: desc.enumerable,
                get: function () {
                    // @ts-ignore
                    return wxpromisify(wx[key], wx);
                }
            });
        }
        else {
            Object.defineProperty(wxp, key, desc);
        }
    }
});
    



export default wxp;